﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace cube
{
    /// <summary>
    /// Lesen und Schreiben der Userdaten in/aus der registry
    /// </summary>
    class registryUserData
    {
        /// <summary>
        /// Erzeugt den User-Account in der registry
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pw"></param>
        /// <param name="userLevel"></param>
        public static void insertUserData(String username, String pw, String userLevel)
        {
            RegistryKey reg = Registry.CurrentUser;
            try
            {
                reg = reg.OpenSubKey("cube", true);

                if (reg == null)
                {
                    reg = Registry.CurrentUser;
                    reg.CreateSubKey("cube");
                    reg = reg.OpenSubKey("cube", true);
                    reg.CreateSubKey("users");
                    reg = reg.OpenSubKey("users", true);
                }

                else
                {
                    reg = reg.OpenSubKey("users", true);
                    if (reg == null)
                    {
                        reg = Registry.CurrentUser;
                        reg = reg.OpenSubKey("cube", true);
                        reg.CreateSubKey("users");
                        reg = reg.OpenSubKey("users", true);
                    }
                }
                reg.CreateSubKey(username);
                reg = reg.OpenSubKey(username, true);
                reg.SetValue("passwort", pw, RegistryValueKind.String);
                reg.SetValue("userLevel", userLevel, RegistryValueKind.String);
            }
            catch { }
            finally
            {
                if (reg != null)
                {
                    reg.Flush();
                    reg.Dispose();
                    reg.Close();
                }
            }
        }
        /// <summary>
        /// Löscht einen User aus der registry
        /// </summary>
        /// <param name="username"></param>
        public static void deleteUserData(String username)
        {
            RegistryKey reg = Registry.CurrentUser;
            try
            {
                reg = reg.OpenSubKey("cube", false);
                reg = reg.OpenSubKey("users", true);
                //reg = reg.OpenSubKey(username, false);
                reg.DeleteSubKeyTree(username, false);
            }
            catch { }
            finally
            {
                if (reg != null)
                {
                    reg.Dispose();
                    reg.Close();
                }
            }
        }
        /// <summary>
        /// Gibt Userdaten des übergebenen Users zurück
        /// </summary>
        /// <param name="username"></param>
        /// <returns>[0]:Username und [1]:Userlevel</returns>
        public static String[] getUserData(String username)
        {
            String[] userData = new String[2];
            userData[0] = ""; userData[1] = "";
            RegistryKey reg = Registry.CurrentUser;
            try
            {
                //reg = reg.OpenSubKey(Properties.Resources.RegistryCubeUserPath+username);
                reg = reg.OpenSubKey("cube", false);
                reg = reg.OpenSubKey("users", false);
                reg = reg.OpenSubKey(username, false);

                if (reg != null)
                {
                    userData[0] = (String)reg.GetValue("passwort");
                    userData[1] = (String)reg.GetValue("userLevel");
                    return userData;
                }
                else
                {
                    //Übergebener User existiert nicht in der registry
                    return userData;
                }
            }
            catch { return userData; }
            finally
            {
                if (reg != null)
                {
                    reg.Dispose();
                    reg.Close();
                }
            }
        }
        /// <summary>
        /// Gibt eine Liste aller in der registry registrierten Accounts von cube zurück
        /// </summary>
        /// <returns></returns>
        public static List<String[]> getAllUsers()
        {
            List<String[]> userList = new List<string[]>();
            String[] users;
            RegistryKey reg = Registry.CurrentUser;
            try
            {
                reg = reg.OpenSubKey("cube", false);
                reg = reg.OpenSubKey("users", false);
                users = reg.GetSubKeyNames();

                for (int i = 0; i < users.Length; i++)
                {
                    String[] userData = new String[2];
                    reg = Registry.CurrentUser;
                    reg = reg.OpenSubKey("cube", false);
                    reg = reg.OpenSubKey("users", false);
                    reg = reg.OpenSubKey(users[i], false);
                    userData[0] = users[i];
                    userData[1] = (String)reg.GetValue("userLevel");
                    userList.Add(userData);
                }
                return userList;
            }
            catch { return userList; }
        }
    }
}
